package org.example.encrypt.interceptor;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import org.example.encrypt.common.MybatisCryptHandler;
import org.springframework.stereotype.Component;

import java.sql.Statement;
import java.util.Properties;

/**
 * 解密拦截器
 *
 * @Author WeiBing
 * @Create 2024/8/12 18:24
 */
@Slf4j
@Component
@Intercepts({
        @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
})
@RequiredArgsConstructor
public class MybatisDecryptInterceptor implements Interceptor {

    private final MybatisCryptHandler handler;

    @Override
    public Object intercept (Invocation invocation) throws Exception {
        // 获取执行mysql执行结果
        Object result = invocation.proceed();

        // 对查询结果进行解密
        return handler.resultDecrypt(result);
    }

    @Override
    public Object plugin (Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties (Properties properties) {
    }

}
